package com.tea.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tea.common.entity.Carousel;
import com.tea.common.mapper.CarouselMapper;
import com.tea.common.service.CarouselService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 轮播图Service实现类
 */
@Service
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Override
    public Page<Carousel> pageCarousel(Page<Carousel> page, String title, Integer status) {
        // 创建查询条件
        LambdaQueryWrapper<Carousel> queryWrapper = Wrappers.lambdaQuery();
        
        // 添加标题查询条件（模糊查询）
        if (StringUtils.hasText(title)) {
            queryWrapper.like(Carousel::getTitle, title);
        }
        
        // 添加状态查询条件
        if (status != null) {
            queryWrapper.eq(Carousel::getStatus, status);
        }
        
        // 按序号升序排序
        queryWrapper.orderByAsc(Carousel::getSequence);
        
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Carousel> listEnabledCarousels() {
        // 创建查询条件
        LambdaQueryWrapper<Carousel> queryWrapper = Wrappers.lambdaQuery();
        // 只查询启用状态的轮播图
        queryWrapper.eq(Carousel::getStatus, 1);
        // 按序号升序排序
        queryWrapper.orderByAsc(Carousel::getSequence);
        
        return this.list(queryWrapper);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        //创建修改的对象
        Carousel carousel = new Carousel();
        carousel.setId(id);
        carousel.setStatus(status);
        //执行修改操作
        return this.updateById(carousel);
    }
} 